//import 'package:hf_flutter/module/login_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:nested/nested.dart';

import '../base_provider.dart';
import '../base_widget.dart';
import '../net/NetHelp.dart';

@optionalTypeArgs
Future<dynamic> gotoWidget(BuildContext context, Widget widget,
    {String routeName, List<SingleChildWidget> providers}) async {
  return gotoRoute(
      context,
      getCupertinoPageRoute(widget,
          providers: providers, routeName: routeName));
}

Future<dynamic> gotoWidgetSingleProvider<T extends CunwControllNotify>(
    BuildContext context, Widget widget,
    {String routeName, T value}) {
  return gotoWidget(context, widget,
      routeName: routeName,
      providers: [ChangeNotifierProvider<T>.value(value: value)]);
}

Future<dynamic> gotoRoute(BuildContext context, CupertinoPageRoute route) {
  return Navigator.push(context, route);
}

Future<dynamic> replaceWidget(BuildContext context, Widget widget,
    {String routeName, List<SingleChildWidget> providers}) {
  return Navigator.pushReplacement(
      context,
      getCupertinoPageRoute(widget,
          providers: providers, routeName: routeName));
}

void popToWidget(BuildContext context, String routeName) {
  Navigator.popUntil(context, (Route<dynamic> route) {
    return routeName == route.settings.name;
  });
}

void popToHome(
  BuildContext context,
) {
  Navigator.popUntil(context, (Route<dynamic> route) {
    return route.isFirst;
  });
}

void popAllToWidget(BuildContext context, Widget widget,
    {String routeName, List<SingleChildWidget> providers}) {
  Navigator.pushAndRemoveUntil(
    context,
    getCupertinoPageRoute(widget, providers: providers, routeName: routeName),
    (Route<dynamic> route) {
      return false;
    },
  );
}

Future<bool> popWidget(BuildContext context, {dynamic obj}) {
  return Navigator.maybePop(context, obj);
}

CupertinoPageRoute getCupertinoPageRoute(Widget widget,
    {String routeName, bool showAnimation, List<SingleChildWidget> providers}) {
  return MCupertinoPageRoute(
      settings: RouteSettings(name: routeName ?? widget.toString()),
      showAnimation: showAnimation,
      builder: (BuildContext context) {
        return BasePage(
          data: providers,
          child: widget,
        );
      });
}

class MCupertinoPageRoute extends CupertinoPageRoute {
  MCupertinoPageRoute({
    WidgetBuilder builder,
    String title,
    RouteSettings settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
    this.showAnimation = true,
  })  : assert(builder != null),
        assert(maintainState != null),
        assert(fullscreenDialog != null),
        assert(opaque),
        super(
            builder: builder,
            settings: settings,
            fullscreenDialog: fullscreenDialog);
  final bool showAnimation;

  @override
  Duration get transitionDuration =>
      (showAnimation ?? true) ? super.transitionDuration : Duration.zero;
}
